package servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebServlet(value = "/servletB")
public class ServletB extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        String key2 = (String) req.getAttribute("key2");
//        System.out.println(key2);

//        String key3 = req.getParameter("key3");
//        System.out.println(key3);
//        System.out.println("servletB");
//
//        // 获取session
//        HttpSession session = req.getSession();
//        String session1 = (String) session.getAttribute("session1");
//        System.out.println(session1);

        ServletContext servletContext = getServletContext();
        // 添加
        servletContext.setAttribute("key1", "value1");
        servletContext.setAttribute("key2", "value2");

        HttpSession session = req.getSession();
        session.setAttribute("key1", "value1");
        session.setAttribute("key2", "value2");


        req.setAttribute("key1", "value1");
        req.setAttribute("key2", "value2");

        req.setAttribute("key1", "value3");
        req.removeAttribute("key2");
    }
}
